﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/assets/libs/layui/css/layui.css" />
    <link rel="stylesheet" href="/assets/module/admin.css">
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<body>
    <!-- 主体部分 -->
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-card-body">
                <!-- 表格工具栏 -->
                <form class="layui-form toolbar searchbar">
                    <div class="layui-form-item">

                        <div class="layui-inline">
                            <label class="layui-form-label">任务组名称:</label>
                            <div class="layui-input-inline">
                                <input name="JobGroupName" class="layui-input" placeholder="输入" type="text" />
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">任务名称:</label>
                            <div class="layui-input-inline">
                                <input name="JobName" class="layui-input" placeholder="输入" type="text" />
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">任务状态:</label>
                            <div class="layui-input-inline">
                                <select name="JobStatus">
                                    <option value=""></option>
                                    <option value="0">禁用</option>
                                    <option value="1">启用</option>
                                </select>
                            </div>
                        </div>

                        <div class="layui-inline">
                            &emsp;
                            <button class="layui-btn icon-btn" lay-filter="TbSearch" lay-submit perm-show="system:autojob:search">
                                <i class="layui-icon">&#xe615;</i>搜索
                            </button>
                            <button class="layui-btn icon-btn" type="button" id="SearchSystemTask" perm-show="system:autojob:search">
                                <i class="layui-icon">&#xe656;</i>查询系统任务队列
                            </button>
                        </div>
                    </div>
                </form>
                <!-- 数据表格 -->
                <table id="gridTable" lay-filter="gridTable"></table>
            </div>
        </div>
    </div>

    <!-- 表格操作列 -->
    <script type="text/html" id="toolbar">
        <a perm-show="system:autojob:add" class="layui-btn layui-btn-sm icon-btn" lay-event="add"><i class="layui-icon">&#xe654;</i>新增</a>
        <a perm-show="system:autojob:edit" class="layui-btn layui-btn-warm layui-btn-sm icon-btn" lay-event="edit"><i class="layui-icon">&#xe642;</i>修改</a>
        <a perm-show="system:autojob:detele" class="layui-btn layui-btn-danger layui-btn-sm icon-btn" lay-event="del"><i class="layui-icon">&#xe640;</i>删除</a>
    </script>

    <!-- 表格工具列 -->
    <script type="text/html" id="tool">
        <a perm-show="system:autojob:logview" class="layui-btn layui-btn-sm" lay-event="log">日志</a>
    </script>

    <!-- js部分 -->
    <script src="/assets/libs/jquery/jquery-3.2.1.min.js"></script>
    <script src="/assets/libs/layui/layui.js"></script>
    <script src="/assets/js/utils.js"></script>
    <script src="/assets/js/main.js"></script>
    <script>

        // 全局变量
        var table;

        layui.use(['layer', 'form', 'table', 'util', 'admin', 'setter', 'tableX','code'], function () {
            var $ = layui.jquery;
            var layer = layui.layer;
            var form = layui.form;
            table = layui.table;
            var util = layui.util;
            var admin = layui.admin;
            var setter = layui.setter;

            // 表格初始化
            layuiTableSet();

            /* 渲染表格 */
            var insTb = table.render({
                elem: '#gridTable',
                url: setter.baseServer + '/AutoJobManage/AutoJob/GetPageListJson',
                page: true,
                toolbar: "#toolbar",
                cols: [[
                    { type: 'checkbox' },
                    { type: 'numbers' },
                    { field: 'JobGroupName', title: '任务组名称', sort: true },
                    { field: 'JobName', title: '任务名称', sort: true },
                    {
                        field: 'JobStatus', title: '执行状态', sort: true, templet: function (d) {
                            if (d.JobStatus == 1) {
                                return '<span class="layui-badge layui-badge-blue">启用</span>';
                            } else {
                                return '<span class="layui-badge layui-badge-red">禁用</span>';
                            }
                        }
                    },
                    { field: 'CronExpression', title: 'Cron表达式', sort: true },
                    { field: 'StartTime', title: '运行开始时间', sort: true },
                    { field: 'EndTime', title: '运行结束时间', sort: true },
                    { field: 'NextStartTime', title: '下次执行时间', sort: true },
                    { fixed: 'right', title: '操作', align: 'center', toolbar: '#tool', width: 100 }
                ]]
            });

            /* 表格搜索 */
            form.on('submit(TbSearch)', function (data) {
                insTb.reload({ where: data.field, page: { curr: 1 } });
                return false;
            });

            /* 表格头工具栏点击事件 */
            table.on('toolbar(gridTable)', function (obj) {
                if (obj.event === 'add') { // 添加
                    showEditModel();
                } else if (obj.event === 'edit') { // 修改
                    var checkRows = table.checkStatus('gridTable');
                    if (checkRows.data.length === 0 || checkRows.data.length > 1) {
                        layer.msg('请选择且只选择一条数据', { icon: 2 });
                        return;
                    }
                    showEditModel(checkRows.data[0]);
                } else if (obj.event === 'del') { // 删除
                    var checkRows = table.checkStatus('gridTable');
                    if (checkRows.data.length === 0) {
                        layer.msg('请选择要删除的数据', { icon: 2 });
                        return;
                    }
                    var ids = checkRows.data.map(function (d) {
                        return d.Id;
                    });
                    doDel({ ids: ids });
                } else if (obj.event == 'export') {
                    // 导出
                    var cols = [[
                        { field: 'Remark', title: '字典描述', width: 300 },
                        { field: 'DictType', title: '字典类型' },
                        {
                            field: 'CreateTime', title: '创建时间', templet: function (d) {
                                return util.toDateString(d.CreateTime);
                            }
                        },
                    ]];
                    layuiTableExport(cols, setter.baseServer + '/AutoJobManage/AutoJob/GetListJson', '定时任务导出')
                }
            });

            table.on('tool(gridTable)', function (obj) {
                var data = obj.data; //获得当前行数据
                var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
                var tr = obj.tr; //获得当前行 tr 的 DOM 对象（如果有的话）

                if (layEvent === 'log')//添加备注
                {
                    var JobName = data.JobName;
                    admin.open({
                        type: 2,
                        title: '任务记录',
                        content: 'autoJobLog.html' + '?JobName=' + JobName,
                        area: ['750px', '685px']
                    });
                }
            });

            /* 显示表单弹窗 */
            function showEditModel(mData) {
                var id = 0;
                if (mData != undefined && mData != null) { id = mData.Id; }
                admin.open({
                    type: 2,
                    area: ['780px', '625px'],
                    title: (mData ? '修改' : '添加'),
                    content: 'autoJobForm.html?id=' + id,
                });
            }

            /* 删除 */
            function doDel(obj) {
                layer.confirm('确定要删除选中数据吗？', {
                    skin: 'layui-layer-admin',
                    shade: .1
                }, function (i) {
                    layer.close(i);
                    var loadIndex = layer.load(2);
                    admin.req('/AutoJobManage/AutoJob/DeleteFormJson', obj.ids ? obj.ids.join(',') : '', function (res) {
                        layer.close(loadIndex);
                        if (1 === res.Tag) {
                            layer.msg("操作成功", { icon: 1 });
                            insTb.reload({ page: { curr: 1 } });
                        } else {
                            layer.msg(res.Message, { icon: 2 });
                        }
                    }, 'post');
                });
            }

            $('#SearchSystemTask').click(function () {

                admin.open({
                    type: 1,
                    title: '系统内任务队列',
                    content: '<table id="gridTable2" lay-filter="gridTable2"></table>',
                    success: function (layero, dIndex) {
                        table.render({
                            elem: '#gridTable2',
                            height: 400,
                            size: 'sm',
                            url: setter.baseServer + '/AutoJobManage/AutoJob/GetAllSTJob',
                            cols: [[
                                { type: 'numbers' },
                                { field: 'WorkerName', title: '任务编号', sort: true },
                                {
                                    title: '详细参数',width:85, templet: function (d) {
                                        return ' <a class="layui-btn layui-btn-xs" lay-event="detail">查看</a>';
                                    }
                                }
                            ]]
                        });
                    }
                });

                table.on('tool(gridTable2)', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
                    var data = obj.data; //获得当前行数据
                    if (obj.event === 'detail') { //查看
                        admin.open({
                            type: 1,
                            title: '原始数据',
                            area:['500px'],
                            content: '<pre class="layui-code">' + JSON.stringify(data, null, 2) + '</pre>',
                            success: function (layero, dIndex) {
                                layui.code();
                            }
                        });
                    } 
                });

            });

        });
    </script>
</body>
</html>
